package com.kalaqia.icon.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.Guideline;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatImageView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;

import com.kalaqia.icon.font.view.IconFontTextView;

/**
 * @author kalaqia 42147393@qq.com
 */
public class IconVerticalView extends ConstraintLayout {

    //<editor-fold desc="view">

    private IconFontTextView mLabel;
    private IconFontTextView mContent;
    private Guideline mGuideline;
    private Guideline guideLeft;
    private Guideline guideRight;
    private ViewStub stubLeft;
    private ViewStub stubRight;
    private AppCompatImageView ivLeft;
    private IconFontTextView ivRight;

    private View viewBg;
    private View viewDivider;

    //</editor-fold>

    private Drawable dividerDrawable;

    private int padding;
    private boolean showDivider;
    private float left;
    private float right;
    private String contentIcon;
    private int imgSize = 0;
    @DrawableRes
    private int drawRes;

    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

    public IconVerticalView(Context context) {
        this(context, null);
    }

    public IconVerticalView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        initAttributeSet(context, attrs);
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_base_vertical_view, this, true);
        mLabel = findViewById(R.id.id_base_vertical_view_label);
        mContent = findViewById(R.id.id_base_vertical_view_content);
        mGuideline = findViewById(R.id.id_base_vertical_guideline);
        guideLeft = findViewById(R.id.id_base_vertical_left);
        guideRight = findViewById(R.id.id_base_vertical_right);
        stubLeft = findViewById(R.id.id_base_vertical_view_stub_left);
        viewBg = findViewById(R.id.id_base_vertical_bg);
        viewDivider = findViewById(R.id.id_base_vertical_divider);
        ivRight = findViewById(R.id.id_base_vertical_right_icon);
        mLabel.setGravity(Gravity.START);
        mContent.setGravity(Gravity.END);
        mLabel.setClickable(false);
        mContent.setClickable(false);
        setClickable(true);
        setFocusable(true);
    }

    public void setGuidelinePercent(float present) {
        ConstraintLayout.LayoutParams lp = (LayoutParams) mGuideline.getLayoutParams();
        lp.guidePercent = present;
    }

    public void setLabel(String label) {
        mLabel.setText(label == null ? "" : label);
    }

    public IconVerticalView setContent(String content, String icon) {
        this.contentIcon = icon;
        return setContent(content);
    }

    public IconVerticalView setContent(String content) {
        String temp = TextUtils.isEmpty(content) ? "" : content;
        if (!TextUtils.isEmpty(contentIcon) && !TextUtils.isEmpty(content)) {
            mContent.setTextValue(content).drawIconRight(contentIcon);
        } else if (!TextUtils.isEmpty(contentIcon)) {
            mContent.setText(contentIcon);
        } else {
            mContent.setText(temp);
        }
        mContent.setTextValue(temp);
        return this;
    }

    public void setContentIcon(String icon) {
        this.contentIcon = icon;
        mContent.drawIconRight(icon);
    }

    protected void initAttributeSet(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconVerticalView);
        int count = a.getIndexCount();
        String content = null;
        contentIcon = null;
        String labelIcon = null;
        String label = null;
        for (int i = 0; i < count; i++) {
            int attr = a.getIndex(i);
            if (attr == R.styleable.IconVerticalView_labelColor) {
                mLabel.setTextColor(a.getColor(attr, ContextCompat.getColor(context, R.color.color_icon_vertical_view)));
            } else if (attr == R.styleable.IconVerticalView_contentColor) {
                mContent.setTextColor(a.getColor(attr, ContextCompat.getColor(context, R.color.color_icon_vertical_view)));
            } else if (attr == R.styleable.IconVerticalView_vertical_size) {
                int textSize = a.getDimensionPixelSize(attr,
                        getResources().getDimensionPixelOffset(R.dimen.dimen_default_icon_vertical));
                mLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
                mContent.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
                ivRight.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
            } else if (attr == R.styleable.IconVerticalView_vertical_label) {
                label = a.getString(R.styleable.IconVerticalView_vertical_label);
            } else if (attr == R.styleable.IconVerticalView_vertical_content) {
                content = a.getString(R.styleable.IconVerticalView_vertical_content);
            } else if (attr == R.styleable.IconVerticalView_vertical_label_icon) {
                labelIcon = a.getString(R.styleable.IconVerticalView_vertical_label_icon);
            } else if (attr == R.styleable.IconVerticalView_vertical_content_icon) {
                contentIcon = a.getString(R.styleable.IconVerticalView_vertical_content_icon);
            } else if (attr == R.styleable.IconVerticalView_guidePercent) {
                setGuidelinePercent(a.getFloat(R.styleable.IconVerticalView_guidePercent, 0.3f));
            } else if (attr == R.styleable.IconVerticalView_label_drawStart) {
                drawRes = a.getResourceId(R.styleable.IconVerticalView_label_drawStart, 0);
            } else if (attr == R.styleable.IconVerticalView_label_drawSize) {
                imgSize = a.getDimensionPixelOffset(R.styleable.IconVerticalView_label_drawSize, 0);
            } else if (attr == R.styleable.IconVerticalView_vertical_show_content_right) {
                boolean isContentRight = a.getBoolean(R.styleable.IconVerticalView_vertical_show_content_right, false);
                LayoutParams contentLp = (LayoutParams) mContent.getLayoutParams();
                contentLp.rightToRight = (isContentRight ? R.id.id_base_vertical_right : LayoutParams.PARENT_ID);
            } else if (attr == R.styleable.IconVerticalView_vertical_padding) {
                padding = a.getDimensionPixelOffset(R.styleable.IconVerticalView_vertical_padding, 0);
            } else if (attr == R.styleable.IconVerticalView_vertical_guide_left) {
                left = a.getFloat(R.styleable.IconVerticalView_vertical_guide_left, (float) 0.05);
                if (left < 0 || left > 1) {
                    left = 0;
                }
                LayoutParams leftLp = (LayoutParams) guideLeft.getLayoutParams();
                leftLp.guidePercent = left;
            } else if (attr == R.styleable.IconVerticalView_vertical_guide_right) {
                right = a.getFloat(R.styleable.IconVerticalView_vertical_guide_right, (float) 0.95);
                if (right < 0 || right > 1) {
                    right = 1;
                }
                LayoutParams rightLp = (LayoutParams) guideRight.getLayoutParams();
                rightLp.guidePercent = right;

            } else if (attr == R.styleable.IconVerticalView_vertical_divider) {
                dividerDrawable = a.getDrawable(R.styleable.IconVerticalView_vertical_divider);
            } else if (attr == R.styleable.IconVerticalView_vertical_show_divider) {
                showDivider = a.getBoolean(R.styleable.IconVerticalView_vertical_show_divider, false);
            } else if (attr == R.styleable.IconVerticalView_vertical_icon_right) {
                String temp = a.getString(R.styleable.IconVerticalView_vertical_icon_right);
                ivRight.setText(TextUtils.isEmpty(temp) ? "" : temp);
            } else if (attr == R.styleable.IconVerticalView_vertical_selectedBackground) {
                int res = a.getResourceId(R.styleable.IconVerticalView_vertical_selectedBackground, 0);
                if (res != 0) {
                    viewBg.setBackgroundResource(res);
                }
            } else if (attr == R.styleable.IconVerticalView_vertical_content_font) {
                mContent.setIconTypeface(a.getResourceId(R.styleable.IconVerticalView_vertical_content_font, 0));
            } else if (attr == R.styleable.IconVerticalView_vertical_label_font) {
                mLabel.setIconTypeface(a.getResourceId(R.styleable.IconVerticalView_vertical_label_font, 0));
            } else if (attr == R.styleable.IconVerticalView_vertical_right_font) {
                ivRight.setIconTypeface(a.getResourceId(R.styleable.IconVerticalView_vertical_right_font, 0));
            } else if (attr == R.styleable.IconVerticalView_vertical_icon_right_color) {
                ivRight.setTextColor(a.getColor(attr, ContextCompat.getColor(context, R.color.color_icon_vertical_view)));
            }
        }
        a.recycle();

        if (imgSize == 0) {
            imgSize = (int) mLabel.getTextSize();
        }

        setContent(content);

        if (!TextUtils.isEmpty(labelIcon) && !TextUtils.isEmpty(label)) {
            mLabel.setTextValue(label).drawIconLeft(labelIcon);
        } else if (!TextUtils.isEmpty(labelIcon)) {
            mLabel.setText(labelIcon);
        } else {
            mLabel.setText(label);
        }
        labelDrawStart();

        if (padding == 0) {
            padding = context.getResources().getDimensionPixelOffset(R.dimen.dimen_default_vertical_padding);
        }

        mContent.setPadding(imgSize == 0 ? padding : 0, padding, padding, padding);
        mLabel.setPadding(imgSize == 0 ? padding : 0, padding, padding, padding);

        setDividerDrawable();

    }

    private void setDividerDrawable() {
        if (!showDivider) {
            viewDivider.setVisibility(View.GONE);
            return;
        }
        viewDivider.setVisibility(View.VISIBLE);
        if (dividerDrawable == null) {
            final TypedArray a = getContext().obtainStyledAttributes(ATTRS);
            dividerDrawable = a.getDrawable(0);
            a.recycle();
        }
        viewDivider.setBackground(dividerDrawable);
    }

    public void labelDrawStart(@DrawableRes int resId) {
        this.drawRes = resId;
        labelDrawStart();
    }

    public void labelDrawStart(Drawable drawable) {

        if (drawable == null) {
            return;
        }

        if (ivLeft == null) {
            View view = stubLeft.inflate();
            ivLeft = view.findViewById(R.id.id_base_vertical_view_iv_left);
            ivLeft.setVisibility(imgSize == 0 ? View.GONE : View.VISIBLE);
            LayoutParams lp = (LayoutParams) ivLeft.getLayoutParams();
            lp.width = lp.height = imgSize;
            LayoutParams lp1 = (LayoutParams) guideLeft.getLayoutParams();
            if (left < 0.15) {
                left = (float) 0.15;
            }
            lp1.guidePercent = left;
        }

        if (ivLeft == null) {
            return;
        }

        ivLeft.setBackgroundDrawable(drawable);

    }

    private void labelDrawStart() {
        if (ivLeft == null && imgSize != 0) {
            View view = stubLeft.inflate();
            ivLeft = view.findViewById(R.id.id_base_vertical_view_iv_left);
        }
        if (ivLeft == null || drawRes == 0) {
            return;
        }
        ivLeft.setVisibility(imgSize == 0 ? View.GONE : View.VISIBLE);
        LayoutParams lp = (LayoutParams) ivLeft.getLayoutParams();
        lp.width = lp.height = imgSize;
        LayoutParams lp1 = (LayoutParams) guideLeft.getLayoutParams();

        if (drawRes != 0 && left < 0.15) {
            left = (float) 0.15;
        }

        lp1.guidePercent = left;
        ivLeft.setBackgroundResource(drawRes);

    }

    public void setContentColor(@ColorInt int color) {
        mContent.setTextColor(color);
    }

    public void setLabelColor(@ColorInt int color) {
        mLabel.setTextColor(color);
    }


}
